package com.pug.web.login;

import com.pug.bo.KssUserBo;
import com.pug.pojo.KssUser;
import com.pug.result.ex.KsdBusinessException;
import com.pug.service.idwork.IdWorkService;
import com.pug.service.user.IUserService;
import com.pug.utils.FastJsonUtil;
import com.pug.utils.fn.asserts.Vsserts;
import com.pug.vo.KssLogoutVo;
import com.pug.vo.KssUserVo;
import com.pug.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.UUID;

/**
 * Description:
 * Author: taimi 37310
 * Version: 1.0
 * Create Date Time: 2022/4/5 4:36.
 * Update Date Time:
 *
 * @see
 */
@Api(tags = "登录管理")
@RestController
@RequiredArgsConstructor
public class PassportLoginController extends BaseController {
    private final IUserService userService;
    private final IdWorkService idWorkService;

    @ApiOperation(value = "退出登录方法")
    @PostMapping("/passport/logout")
    public String toLogout(@RequestBody  KssLogoutVo kssLogoutVo) {
        redisOperator.del(PUG_XQ_LOGIN_USER_INFO+kssLogoutVo.getUserId());
        redisOperator.del(PUG_XQ_LOGIN_USER_TOKEN+kssLogoutVo.getToken());

        return "success";
    }



    @ApiOperation(value = "登录方法")
    @PostMapping("/passport/login")
    public KssUserBo toLoginReg (@RequestBody @Valid KssUserVo kssUserVo){
        // 1: 接受用户提交用户手机和短信
        String inputPhone = kssUserVo.getPhone();
        String inputSmsCode = kssUserVo.getSmscode();
        // 2: 获取redis缓存中的存储短信
        String cacheSmsCode = redisOperator.get(PUG_XQ_LOGIN_SMS_CODE + inputPhone);
        Vsserts.isEmptyEx(cacheSmsCode, "短信已经失效");
        // 3: 对比短信码是否正确
        if (!cacheSmsCode.equalsIgnoreCase(inputSmsCode)) {
            throw new KsdBusinessException(602, "你输入短信证码有误!");
        }
        // 4：开始进行业务的处理
        KssUser dbUser = userService.getByPhone(inputPhone);
        // 如果dbUser，说明没有注册
        if (Vsserts.isNull(dbUser)) {
            // 开始注册
            dbUser = new KssUser();
            dbUser.setPhone(inputPhone);
            dbUser.setIdcode(idWorkService.nextShort());
            dbUser.setNickname("小伴");
            dbUser.setPassword("");
            dbUser.setAvatar("aa.jpg");
            dbUser.setSex(2);
            dbUser.setCountry("中国");
            dbUser.setProvince("");
            dbUser.setCity("");
            dbUser.setDistrict("");
            dbUser.setDescription("Ta什么都没有留下...");
            dbUser.setBgImg("biimg.jpg");
            dbUser.setStatus(1);
            userService.saveOrUpdate(dbUser);
        }

        // 5: 存在就返回
        KssUserBo kssUserBo = new KssUserBo();
        BeanUtils.copyProperties(dbUser, kssUserBo);
        // 6: 生成一个uuid代表token
        String tokenUuid = UUID.randomUUID().toString();
        kssUserBo.setToken(tokenUuid);

        //7.登录成功把token,kssUserBo缓存到redis中

        redisOperator.set(PUG_XQ_LOGIN_USER_INFO+kssUserBo.getId(),
                FastJsonUtil.toJSON(kssUserBo));
        redisOperator.set(PUG_XQ_LOGIN_USER_TOKEN+kssUserBo.getId(),
                tokenUuid);


        //8.删除短信验证码
        redisOperator.del(PUG_XQ_LOGIN_SMS_CODE + inputPhone);





        return kssUserBo;
    }


}
